package com.wephone.Student.network;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.util.PullToRefreshView;

public class MyResponseHandler extends AsyncHttpResponseHandler {
	protected Gson gson = new Gson();
	protected Class<?> classzz;
	protected BaseResponse response;
	protected boolean getDataSuccess = false;
	private Activity activity;
	
	public MyResponseHandler(Activity activity, Class<?> classzz) {
		super();
		this.activity = activity;
		this.classzz = classzz;
	}

	@Override
	public void onSuccess(String result) {
		System.out.println("---result success---"+result);
		View progress = activity.findViewById(R.id.progressBar);
		if(progress!=null){
			progress.setVisibility(View.GONE);
		}
		View pullRefreshView = activity.findViewById(R.id.pull_refresh_listview);
		if(pullRefreshView!=null){
			((PullToRefreshView)pullRefreshView).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView).onFooterRefreshComplete();
		}
		View pullRefreshView1 = activity.findViewById(R.id.pull_refresh_listview1);
		if(pullRefreshView1!=null){
			((PullToRefreshView)pullRefreshView1).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView1).onFooterRefreshComplete();
		}
		View pullRefreshView2 = activity.findViewById(R.id.pull_refresh_listview2);
		if(pullRefreshView2!=null){
			((PullToRefreshView)pullRefreshView2).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView2).onFooterRefreshComplete();
		}
		View pullRefreshView7 = activity.findViewById(R.id.pull_refresh_listview7);
		if(pullRefreshView7!=null){
			((PullToRefreshView)pullRefreshView7).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView7).onFooterRefreshComplete();
		}
		View pullRefreshView8 = activity.findViewById(R.id.pull_refresh_listview8);
		if(pullRefreshView8!=null){
			((PullToRefreshView)pullRefreshView8).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView8).onFooterRefreshComplete();
		}
		View pullRefreshView10 = activity.findViewById(R.id.pull_refresh_listview10);
		if(pullRefreshView10!=null){
			((PullToRefreshView)pullRefreshView10).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView10).onFooterRefreshComplete();
		}
		View pullRefreshView3 = activity.findViewById(R.id.pull_refresh_listview3);
		if(pullRefreshView3!=null){
			((PullToRefreshView)pullRefreshView3).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView3).onFooterRefreshComplete();
		}
		if(!TextUtils.isEmpty(result)){
			try{
				response = (BaseResponse)gson.fromJson(result, classzz);
				System.out.println("---message---"+response.getErrorMsg());
				if(response.getErrNo()!=null && !response.getErrNo().equals("0")){
					getDataSuccess = false;
				//	showNetworkErrorMsg(response.getErrorMsg());
				}else{
					getDataSuccess = true;
				}
			}catch(Exception e){
				e.printStackTrace();
				getDataSuccess = false;
				//showNetworkErrorMsg();
			}
		}
	}
	
	@Override
	public void onFailure(Throwable arg0, String result) {
		System.out.println("---result failure---"+result);
		View progress = activity.findViewById(R.id.progressBar);
		if(progress!=null){
			progress.setVisibility(View.GONE);
		}
		View pullRefreshView = activity.findViewById(R.id.pull_refresh_listview);
		if(pullRefreshView!=null){
			((PullToRefreshView)pullRefreshView).onHeaderRefreshComplete();
			((PullToRefreshView)pullRefreshView).onFooterRefreshComplete();
		}
		showNetworkErrorMsg();
	}

	private void showNetworkErrorMsg(){
		Toast.makeText(activity, activity.getString(R.string.network_error_txt), Toast.LENGTH_SHORT).show();
	}
	
	private void showNetworkErrorMsg(String msg){
		if(!TextUtils.isEmpty(msg)){
			Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
		}
	}
}
